package sk.stuba.fiit.ltasys.web.mvc.controller;

/**
 * Archive Controller.
 * 
 * @author Marek Hruska
 */
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sk.stuba.fiit.ltasys.web.mvc.model.entity.ArchivePackageEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.EvidenceRecordEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.UserEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.ArchiveService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.UserService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.core.ArchiveSignatureVerifier;


/**
 * Archive controller.
 * 
 * @author Marek Hruska
 */
@Controller
public class ArchiveController {
	
	private static final String ARCHIVESOFUSER                   = "archivesOfUser";
	private static final String EVIDENCERECORDSOFARCHIVE         = "evidenceRecordsOfArchive";
	private static final String EVIDENCERECORD                   = "evidenceRecord";
	private static final String CONTROLLEDARCHIVE                = "controlledArchive";
	private static final String ISVALID                          = "isValid";
	
	private static final String LISTOFARCHIVEVIEW                = "archive/listOfArchives";
	private static final String LISTOFEVIDENCERECORDSVIEW        = "evidenceRecord/listOfEvidenceRecord";
	private static final String DETAILOFEVIDENCERECORDSVIEW      = "evidenceRecord/detailOfEvidenceRecord";

	
	protected static Logger logger = Logger.getLogger("ArchiveController"); 
	
	@Autowired()
	private ArchiveService archiveService;	
	
	@Autowired()
	private UserService userService;
	
	@Autowired
	private ArchiveSignatureVerifier archiveSignature;
	
	
    @RequestMapping(value="/archive/listOfArchives", method=RequestMethod.GET)
    public ModelAndView getListOfArchives() {
    	logger.debug("Received request to show all archives of user"); 
    	ModelAndView mv = new ModelAndView();

    	//get logged User
    	User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserEntity userEntity = userService.findUserByUserName(user.getUsername());
    	
        //set into model archives of user
        mv.addObject(ARCHIVESOFUSER, archiveService.getAllArchivesByUser(userEntity));
    	mv.setViewName(LISTOFARCHIVEVIEW);
    	
        return mv;
    }
    
    @RequestMapping(value="/evidenceRecord/listOfEvidenceRecords", method=RequestMethod.GET)
    public ModelAndView getListOfEvidenceRecords(@RequestParam(value = "token", required = false) String token) {
    	logger.debug("Received request to show all evidence records of archive"); 
    	ModelAndView mv = new ModelAndView();
    	
    	//set into model evidence records of archive
        mv.addObject(EVIDENCERECORDSOFARCHIVE, archiveService.loadArchivePackageByToken(token).getArchiveRecord());
        mv.setViewName(LISTOFEVIDENCERECORDSVIEW);
    	
        return mv;
    }
    
    @RequestMapping(value="/evidenceRecord/detailOfEvidenceRecords.html", method=RequestMethod.GET)
    public ModelAndView getDetailOfEvidenceRecords(@RequestParam(value = "record_id", required = true) int record_id) {
    	logger.debug("Received request to show all evidence records of archive"); 
    
    	ModelAndView mv = new ModelAndView();
    	EvidenceRecordEntity archiveRecord = archiveService.getArchiveRecordById(record_id);
    	mv.addObject(EVIDENCERECORD, archiveRecord);
    	mv.setViewName(DETAILOFEVIDENCERECORDSVIEW);
    	
        return mv;
    }
    
    @RequestMapping(value="/archive/delete.html", method=RequestMethod.GET)
    public String deleteArchive (@RequestParam(value = "token", required = false) String token) {
    	logger.debug("Received request to delete archive"); 
    
    	archiveService.deleteArchivePackage(archiveService.loadArchivePackageByToken(token));
    	return "redirect:/archive/listOfArchives.html";
    }
    
    @RequestMapping(value="/archive/validate.html", method=RequestMethod.GET)
    public ModelAndView validateArchive (@RequestParam(value = "token", required = false) String token) {
    	Boolean isValid = null;
    	logger.debug("Received request to show all archives of user"); 
    	ModelAndView mv = new ModelAndView();

    	//get logged User
    	User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserEntity userEntity = userService.findUserByUserName(user.getUsername());
    	
        //set into model archives of user
        mv.addObject(ARCHIVESOFUSER, archiveService.getAllArchivesByUser(userEntity));
        
        //Archive to control
        ArchivePackageEntity archiveForControl = archiveService.loadArchivePackageByToken(token);
        try{
        archiveSignature.verifieArchiveSignature(archiveForControl);
        isValid = Boolean.TRUE;
        }catch(Exception e){
        	isValid = Boolean.FALSE;     
        	e.printStackTrace();
        }
        mv.addObject(ISVALID, isValid);
        mv.addObject(CONTROLLEDARCHIVE, archiveForControl);
        
    	mv.setViewName(LISTOFARCHIVEVIEW);
    	return mv;
    }
}
